package com.yiwei.jmx.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.jmx.R;
import com.yiwei.jmx.activity.ApplyStatusIngActivity;
import com.yiwei.jmx.activity.BorrowStepActivity;
import com.yiwei.jmx.activity.LoginTotalActivity;
import com.yiwei.jmx.activity.RepayMoneyActivity;
import com.yiwei.jmx.activity.YmdBorrowActivity;
import com.yiwei.jmx.api.model.DealIndexModel;
import com.yiwei.jmx.constant.UserInfoGlobal;
import com.yiwei.jmx.event.ChangeHomePageEvent;
import com.yiwei.jmx.util.UI;
import com.yiwei.jmx.util.Util;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class ChannelTypeAdapter extends ItemAdapter<DealIndexModel.DataBean.ChannelBean, ChannelTypeAdapter.DefaultHolder> {


    private Context context;

    public ChannelTypeAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_channel_type, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        DealIndexModel.DataBean.ChannelBean item = getItem(position);

        // -1:没有申请记录 0：申请中，1：招标中，2：待放款，3：审核失败，4：还款中，5：已还清
        switch (item.deal_status) {
            case "-1":
            case "5":
                if (item.is_auth.equals("1")) {
                    holder.txtTips.setText("当前可借额度(元)");
                    holder.txtMoney.setText(item.auth_money);
                    holder.txtMoney.setTextColor(0xff0072FF);
                    holder.btnTotal.setVisibility(View.GONE);
                    holder.btnOne.setVisibility(View.VISIBLE);
                    holder.btnTwo.setVisibility(View.VISIBLE);
                    holder.btnOne.setText("去借钱");
                    holder.btnOne.setShapeSolidColor(0xff0072FF).setUseShape();
                    holder.btnTwo.setText("看额度");
                    holder.btnTwo.setTextColor(0xff0072FF);
                    holder.btnTwo.setShapeSolidColor(0xffffffff).setShapeStrokeColor(0xff0072FF).setShapeStrokeWidth(1).setUseShape();
                    holder.layoutIdentity.setVisibility(View.VISIBLE);
                    holder.txtIdentityInfo.setText(UserInfoGlobal.getUserNick() + "，恭喜您已经成功授权本平台征信数据！");
                } else {
                    holder.txtTips.setText("最高可借额度(元)");
                    holder.txtMoney.setText(item.money_max);
                    holder.txtMoney.setTextColor(0xff0072FF);
                    holder.btnTotal.setVisibility(View.VISIBLE);
                    holder.btnTotal.setText("去申请");
                    holder.btnTotal.setShapeSolidColor(0xff0072FF).setUseShape();
                    holder.btnOne.setVisibility(View.GONE);
                    holder.btnTwo.setVisibility(View.GONE);
                    holder.layoutIdentity.setVisibility(View.GONE);
                }
                holder.txtBorrowTips.setVisibility(View.GONE);
                break;
            case "0":
            case "1":
                holder.btnTotal.setVisibility(View.GONE);
                holder.btnOne.setVisibility(View.VISIBLE);
                holder.btnTwo.setVisibility(View.VISIBLE);
                holder.btnOne.setText("提智信分");
                holder.btnOne.setShapeSolidColor(0xff0072FF).setUseShape();
                holder.btnTwo.setText("查看进度");
                holder.btnTwo.setTextColor(0xff0072FF);
                holder.btnTwo.setShapeSolidColor(0xffffffff).setShapeStrokeColor(0xff0072FF).setShapeStrokeWidth(1).setUseShape();
                holder.layoutIdentity.setVisibility(View.VISIBLE);
                holder.txtBorrowTips.setVisibility(View.GONE);
                holder.txtIdentityInfo.setText(UserInfoGlobal.getUserNick() + "，您的借款申请正在审核中...");
                holder.txtTips.setText("当前可借额度(元)");
                holder.txtMoney.setText(item.auth_money);
                holder.txtMoney.setTextColor(0xff0072FF);
                break;
            case "2":
                holder.btnTotal.setVisibility(View.GONE);
                holder.btnOne.setVisibility(View.VISIBLE);
                holder.btnTwo.setVisibility(View.VISIBLE);
                holder.btnOne.setText("放款中");
                holder.btnOne.setShapeSolidColor(0xffFF8400).setUseShape();
                holder.btnTwo.setText("提智信分");
                holder.btnTwo.setTextColor(0xffFFFFFF);
                holder.btnTwo.setShapeSolidColor(0xff0072FF).setUseShape();
                holder.layoutIdentity.setVisibility(View.VISIBLE);
                holder.txtBorrowTips.setVisibility(View.GONE);
                holder.txtIdentityInfo.setText(UserInfoGlobal.getUserNick() + "，恭喜您！您的借款申请审核通过啦！");
                holder.txtTips.setText("当前可借额度(元)");
                holder.txtMoney.setText(item.auth_money);
                holder.txtMoney.setTextColor(0xff0072FF);
                break;
            case "3":
                if (item.deal.next_apply_time.equals("0")) {
                    if (item.is_auth.equals("1")) {
                        holder.txtTips.setText("当前可借额度(元)");
                        holder.txtMoney.setText(item.auth_money);
                        holder.txtMoney.setTextColor(0xff0072FF);
                        holder.btnTotal.setVisibility(View.GONE);
                        holder.btnOne.setVisibility(View.VISIBLE);
                        holder.btnTwo.setVisibility(View.VISIBLE);
                        holder.btnOne.setText("去借钱");
                        holder.btnOne.setShapeSolidColor(0xff0072FF).setUseShape();
                        holder.btnTwo.setText("看额度");
                        holder.btnTwo.setTextColor(0xff0072FF);
                        holder.btnTwo.setShapeSolidColor(0xffffffff).setShapeStrokeColor(0xff0072FF).setShapeStrokeWidth(1).setUseShape();
                        holder.layoutIdentity.setVisibility(View.VISIBLE);
                        holder.txtIdentityInfo.setText(UserInfoGlobal.getUserNick() + "，恭喜您已经成功授权本平台征信数据！");
                    } else {
                        holder.txtTips.setText("最高可借额度(元)");
                        holder.txtMoney.setText(item.money_max);
                        holder.txtMoney.setTextColor(0xff0072FF);
                        holder.btnTotal.setVisibility(View.VISIBLE);
                        holder.btnTotal.setText("去申请");
                        holder.btnTotal.setShapeSolidColor(0xff0072FF).setUseShape();
                        holder.btnOne.setVisibility(View.GONE);
                        holder.btnTwo.setVisibility(View.GONE);
                        holder.layoutIdentity.setVisibility(View.GONE);
                    }
                    holder.txtBorrowTips.setVisibility(View.GONE);
                } else {
                    holder.btnTotal.setVisibility(View.GONE);
                    holder.btnOne.setVisibility(View.VISIBLE);
                    holder.btnTwo.setVisibility(View.VISIBLE);
                    holder.btnOne.setText("提智信分");
                    holder.btnOne.setShapeSolidColor(0xff0072FF).setUseShape();
                    holder.btnTwo.setText("查看详细");
                    holder.btnTwo.setTextColor(0xff0072FF);
                    holder.btnTwo.setShapeSolidColor(0xffffffff).setShapeStrokeColor(0xff0072FF).setShapeStrokeWidth(1).setUseShape();
                    holder.layoutIdentity.setVisibility(View.VISIBLE);
                    holder.txtBorrowTips.setVisibility(View.VISIBLE);
                    holder.txtBorrowTips.setTextColor(0xff566F8E);
                    if (!TextUtils.isEmpty(item.deal.mesg)) {
                        holder.txtBorrowTips.setText(item.deal.mesg);
                    } else {
                        holder.txtBorrowTips.setText("很遗憾！您的借款申请审核未通过，请过段时间再来申请吧");
                    }

                    holder.txtIdentityInfo.setText(UserInfoGlobal.getUserNick() + "，您的借款申请审核未通过！");
                    holder.txtTips.setText("申请金额(元)");
                    holder.txtMoney.setText(item.deal.money);
                    holder.txtMoney.setTextColor(0xff0072FF);
                }

                break;
            case "4":
                if (Float.valueOf(item.deal.break_day) > 0) {
                    holder.btnTotal.setVisibility(View.VISIBLE);
                    holder.btnTotal.setText("立即还款");
                    holder.btnTotal.setShapeSolidColor(0xffFF2740).setUseShape();
                    holder.btnOne.setVisibility(View.GONE);
                    holder.btnTwo.setVisibility(View.GONE);

                    holder.layoutIdentity.setVisibility(View.VISIBLE);
                    holder.txtIdentityInfo.setText(UserInfoGlobal.getUserNick() + "，您近期有一笔待还款金额喔!");
                    holder.txtTips.setText("待还金额(元)");

                    holder.txtMoney.setText(item.deal.money);
                    holder.txtMoney.setTextColor(0xffFF2740);
                    holder.txtBorrowTips.setVisibility(View.VISIBLE);
                    holder.txtBorrowTips.setTextColor(0xffFF2740);
                    holder.txtBorrowTips.setText("当前借款已逾期" + item.deal.break_day + "天！为避免您的个人信用，请立即还款！");
                    break;
                } else {
                    holder.btnTotal.setVisibility(View.GONE);
                    holder.btnOne.setVisibility(View.VISIBLE);
                    holder.btnTwo.setVisibility(View.VISIBLE);
                    holder.btnOne.setText("提前还款");
                    holder.btnOne.setShapeSolidColor(0xff10BC69).setUseShape();
                    holder.btnTwo.setText("提智信分");
                    holder.btnTwo.setTextColor(0xffffffff);
                    holder.btnTwo.setShapeSolidColor(0xff0072FF).setUseShape();
                    holder.layoutIdentity.setVisibility(View.VISIBLE);
                    holder.txtIdentityInfo.setText(UserInfoGlobal.getUserNick() + "，您近期有一笔待还款金额喔！");
                    holder.txtTips.setText("待还金额(元)");
                    holder.txtMoney.setTextColor(0xff10BC69);
                    holder.txtMoney.setText(item.deal.money);
                    holder.txtBorrowTips.setVisibility(View.VISIBLE);
                    holder.txtBorrowTips.setTextColor(0xff10BC69);
                    holder.txtBorrowTips.setText("还款日期：" + item.deal.repay_date);
                }


        }
        if (item.item != null && item.item.size() != 0) {
            HomeChannelAdapter adapter = new HomeChannelAdapter(context);
            holder.listChannel.setLayoutManager(new GridLayoutManager(context, 2));
            holder.listChannel.setAdapter(adapter);
            holder.listChannel.setNestedScrollingEnabled(false);
            adapter.update(item.item);
        } else {
            holder.listChannel.setVisibility(View.GONE);
        }


        holder.btnTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (UI.isFastClick()) {
                    return;
                }
                if (!UserInfoGlobal.isLogin()) {
                    Util.startActivity(context, LoginTotalActivity.class);
                    return;
                }
                if (UI.toString(holder.btnTotal).equals("去申请")) {
                    Intent intent = new Intent(context, YmdBorrowActivity.class);
                    intent.putExtra("company_id", item.company_id);
                    intent.putExtra("channel_id", item.channel_id);
                    context.startActivity(intent);
                } else if (UI.toString(holder.btnTotal).equals("立即还款")) {

                    Intent intent = new Intent(context, RepayMoneyActivity.class);
                    intent.putExtra("deal_id", item.deal.deal_id);
                    context.startActivity(intent);
                }
            }
        });
        holder.btnOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (UI.isFastClick()) {
                    return;
                }
                if (!UserInfoGlobal.isLogin()) {
                    Util.startActivity(context, LoginTotalActivity.class);
                    return;
                }
                if (UI.toString(holder.btnOne).equals("去借钱")) {
                    if (item.is_auth.equals("1")) {
                        Intent intent = new Intent(context, BorrowStepActivity.class);
                        intent.putExtra("company_id", item.company_id);
                        intent.putExtra("channel_id", item.channel_id);
                        context.startActivity(intent);
                    } else {
                        Intent intent = new Intent(context, YmdBorrowActivity.class);
                        intent.putExtra("company_id", item.company_id);
                        intent.putExtra("channel_id", item.channel_id);
                        context.startActivity(intent);
                    }

                } else if (UI.toString(holder.btnOne).equals("立即还款")) {
                    Intent intent = new Intent(context, RepayMoneyActivity.class);
                    intent.putExtra("deal_id", item.deal.deal_id);
                    context.startActivity(intent);

                } else if (UI.toString(holder.btnOne).equals("提智信分")) {
                    EventBus.getDefault().post(new ChangeHomePageEvent(2));
                } else if (UI.toString(holder.btnOne).equals("放款中")) {

                } else if (UI.toString(holder.btnOne).equals("提前还款")) {
                    Intent intent = new Intent(context, RepayMoneyActivity.class);
                    intent.putExtra("deal_id", item.deal.deal_id);
                    context.startActivity(intent);
                }
            }
        });

        holder.btnTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (UI.isFastClick()) {
                    return;
                }
                if (!UserInfoGlobal.isLogin()) {
                    Util.startActivity(context, LoginTotalActivity.class);
                    return;
                }
                if (UI.toString(holder.btnTwo).equals("看额度")) {
                    Intent intent = new Intent(context, YmdBorrowActivity.class);
                    intent.putExtra("company_id", item.company_id);
                    intent.putExtra("channel_id", item.channel_id);
                    context.startActivity(intent);
                } else if (UI.toString(holder.btnTwo).equals("查看进度")) {
                    Intent intent = new Intent(context, ApplyStatusIngActivity.class);
                    intent.putExtra("deal_id", item.deal.deal_id);
                    context.startActivity(intent);
                } else if (UI.toString(holder.btnTwo).equals("提智信分")) {
                    EventBus.getDefault().post(new ChangeHomePageEvent(2));
//                    Intent intent = new Intent(context, YmdBorrowActivity.class);
//                    intent.putExtra("company_id", item.company_id);
//                    intent.putExtra("channel_id", item.channel_id);
//                    context.startActivity(intent);
                } else if (UI.toString(holder.btnTwo).equals("查看详细")) {
                    Intent intent = new Intent(context, ApplyStatusIngActivity.class);
                    intent.putExtra("deal_id", item.deal.deal_id);
                    context.startActivity(intent);
                }
//                else if (UI.toString(holder.btnTwo).equals("查看进度")) {
//                    Intent intent = new Intent(context, YmdBorrowActivity.class);
//                    intent.putExtra("company_id", item.company_id);
//                    intent.putExtra("channel_id", item.channel_id);
//                    context.startActivity(intent);
//                }
            }
        });

        holder.txtName.setText(item.name);


    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_logo)
        ImageView imgLogo;
        @BindView(R.id.txt_name)
        TextView txtName;
        @BindView(R.id.layout_identity)
        LinearLayout layoutIdentity;
        @BindView(R.id.txt_tips)
        TextView txtTips;
        @BindView(R.id.txt_money)
        TextView txtMoney;
        @BindView(R.id.btn_total)
        SuperButton btnTotal;
        @BindView(R.id.btn_one)
        SuperButton btnOne;
        @BindView(R.id.btn_two)
        SuperButton btnTwo;
        @BindView(R.id.txt_borrow_tips)
        TextView txtBorrowTips;
        @BindView(R.id.list_channel)
        RecyclerView listChannel;
        @BindView(R.id.layout_total)
        LinearLayout layoutTotal;
        @BindView(R.id.txt_identity_info)
        TextView txtIdentityInfo;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onSuccess();
    }
}
